// pages/address/newaddress/newaddress.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stationVisible: false,
    stationList: [],
    keyId: '',
    adressName: '',
    adressPhone: '',
    adressStationNo: '',
    adressStationName: '请选择',
    isSave: true,
    prePage: ''//下单页面传过来的标识，1表示寄件信息；2表示收件信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let data = getApp().globalData.pageData[constants.page_newaddress]
    if (data) {
      this.getDictData()
      if (data.prePage) {
        this.setData({
          prePage: data.prePage,
          isSave: data.isSave || false,
          adressName: data.adressName || '',
          adressPhone: data.adressPhone || '',
          adressStationNo: data.adressStationNo || '',
          adressStationName: data.adressStationName || '',
          keyId: data.keyId || '',
        })
      } else {
        this.setData({
          prePage: '',
          isSave: true,
          adressName: data.adressName || '',
          adressPhone: data.adressPhone || '',
          adressStationNo: data.adressStationNo || '',
          adressStationName: data.adressStationName || '',
          keyId: data.keyId || '',
        })
      }
    }
  },

  getDictData() {
    request.get('/HomeController/getDictData', {dictCode: constants.dict.客运站}).then(res => {
      if (res.data.code == constants.resultSuccessCode &&
          res.data.result) {
        let dictData = res.data.result[constants.dict.客运站] || []
        let list = []
        dictData.forEach(item => {
          let obj = {}
          obj['label'] = item['itemText'] || ''
          obj['value'] = item['itemValue'] || ''
          list.push(obj)
        });

        this.setData({
          stationList: list
        })
      }
    })
  },
  onInput(e) {
    let name = e.currentTarget.dataset.name
    let value = e.detail.value
    this.setData({
      [`${name}`]: value
    })
  },
  skipBack() {
    if (!this.data.adressName || !this.data.adressPhone
      || !this.data.adressStationNo) {
        wx.showToast({
          title: '信息需录入完整',
          icon: 'error',
          duration: 2000
        })
      return 
    }
    if (this.data.isSave) {
      this.saveUserAddress()
    } else {
      if (this.data.prePage) {
        getApp().globalData.pageData[constants.order_address] = this.data
      }
      wx.navigateBack({})
    }
    
  },

  saveUserAddress() {
    let user = getApp().getUserInfo()
    let data = Object.assign({}, this.data, user)
    request.post('/UserController/saveAddress', data).then(res => {
      if (res.data.code == constants.resultSuccessCode) {
        if (this.data.prePage) {
          let data = Object.assign({}, this.data, {keyId: res.data.result.keyId})
          getApp().globalData.pageData[constants.order_address] = data
        }
        wx.navigateBack({})
      }
      
    })

  },

  onStationPicker() {
    this.setData({
      stationVisible: true
    })
  },
  onColumnChange(e) {

  },

  onPickerChange(e) {
    const { value } = e.detail;
    const { label } = e.detail;

    this.setData({
      adressStationNo: value[0],
      adressStationName: label[0],
      stationVisible: false
    })
  },
  onPickerCancel(e) {
    this.setData({
      stationVisible: false
    })
  },

  onChange(e) {
    let status = e.detail.checked
    this.setData({
      isSave: status
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})